package com.lby.graduation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lby.graduation.entity.Cart;
import com.lby.graduation.entity.Goods;
import com.lby.graduation.mapper.CartMapper;
import com.lby.graduation.service.CartService;
import com.lby.graduation.service.GoodsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李兵耀
 * @since 2021-01-08
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Resource
    private GoodsService goodsService;
    @Resource
    public CartService cartService;

    /*加入购物车*/
    @GetMapping("/addCart")
    public boolean addCart(Cart cart) {
        List<Cart> carts = cartService.list(new QueryWrapper<Cart>().eq("goods_id", cart.getGoodsId()).eq("user_id", cart.getUserId()));
        if(carts.size()==0){
            return cartService.save(cart);
        }else{
            return false;
        }

    }

    /*查询购物车里的商品*/
    @GetMapping("/selectCartByUserId")
    public List<Goods> selectCartByUserId(Integer userId) {
       return goodsService.selectCartByUserId(userId);
      //  return null;

    }

    /*删除购物车*/
    /*查询购物车里的商品*/
    @GetMapping("/deleteCart")
    public boolean deleteCart(Integer goodsId) {
        return cartService.remove(new QueryWrapper<Cart>().eq("goods_id",goodsId));

    }

}
